//
// Created by 崔世强 on 2018/3/20.
// Copyright (c) 2018 Facebook. All rights reserved.
//

import Foundation
import WCDBSwift

class ContactDao {
    
    static func updateAllDeleted() throws{
        let db = SQLiteHelper.getDb()
        let friendForUpdate = Contact()
        friendForUpdate.deleted = 1
        try db.update(table: Contact.table, on: Contact.CodingKeys.deleted, with: friendForUpdate)
    }
    
    static func updateAllFriendDeleted() throws{
        let db = SQLiteHelper.getDb()
        let friendForUpdate = Contact()
        friendForUpdate.deleted = 1
        try db.update(table: Contact.table, on: Contact.CodingKeys.deleted, with: friendForUpdate,
                      where: Contact.CodingKeys.friend == true && Contact.CodingKeys.family == false && Contact.CodingKeys.role == Role.normal)
    }
    
    
    static func saveOrUpdateNormalInfo(contact:Contact,and:[PropertyConvertible]) throws {
        var updateFields:[PropertyConvertible] = [Contact.CodingKeys.me,Contact.CodingKeys.name,Contact.CodingKeys.remarkName,Contact.CodingKeys.avatar,Contact.CodingKeys.lastUpdTime]
        if !and.isEmpty {
            updateFields = updateFields + and
        }
        let db = SQLiteHelper.getDb()
        if let contactInDb:Contact = try db.getObject(on: Contact.CodingKeys.all, fromTable: Contact.table, where: Contact.CodingKeys.userId == contact.userId) {
            try db.update(table: Contact.table,
                          on: updateFields,
                          with: contact,where: Contact.CodingKeys.userId == contact.userId)
        }else {
            try db.insert(contact, intoTable: Contact.table)
        }
    }
    
    static func saveOrUpdate(contact:Contact,updateFields:[PropertyConvertible]) throws {
        let db = SQLiteHelper.getDb()
        if let contactInDb:Contact = try db.getObject(on: Contact.CodingKeys.all, fromTable: Contact.table, where: Contact.CodingKeys.userId == contact.userId) {
            try db.update(table: Contact.table,
                          on: updateFields,
                          with: contact,where: Contact.CodingKeys.userId == contact.userId)
        }else {
            try db.insert(contact, intoTable: Contact.table)
        }
    }
    
    
    
    static func getAll() throws -> [Contact] {
        let db = Database(at: AppStatusManager.instance.urlForUserDB ?? URL(fileURLWithPath: ""))
        let contacts:[Contact] = try db.getObjects(on: Contact.CodingKeys.all, fromTable: Contact.table,
                                                   where: Contact.CodingKeys.friend == true && Contact.CodingKeys.deleted == false,
                                                   orderBy: [Contact.CodingKeys.name.asExpression().asOrder()])
        return contacts
    }
    
    class func getMe() throws -> Contact? {
        let db = SQLiteHelper.getDb()
        let contact:Contact? = try db.getObject(on: Contact.CodingKeys.all, fromTable: Contact.table,where: Contact.CodingKeys.me == true)
        return contact
    }
    
    
    class func getContact(_ id: Int) throws -> Contact? {
        let db = SQLiteHelper.getDb()
        let contact:Contact? = try db.getObject(on: Contact.CodingKeys.all, fromTable: Contact.table,where: Contact.CodingKeys.userId == id)
        return contact
    }
    
    static func getAllContact() throws -> [Contact] {
        let db = SQLiteHelper.getDb()
        let contactList:[Contact] = try db.getObjects(on: Contact.CodingKeys.all, fromTable: Contact.table,where: Contact.CodingKeys.deleted == 0 && Contact.CodingKeys.friend == true)
        return contactList
    }
    
    //获取直系联系人
    static func getDirectContact() throws -> [Contact] {
        let db = SQLiteHelper.getDb()
        let contactList: [Contact] = try db.getObjects(on: Contact.CodingKeys.all, fromTable: Contact.table,where:
                                                        (Contact.CodingKeys.deleted == 0) &&
                                                        (Contact.CodingKeys.role == Role.direct || Contact.CodingKeys.role == Role.parent))
        return contactList
    }
    
    //获取家族成员列表
    static func getFamilyMembersList() throws -> [Contact] {
        let db = SQLiteHelper.getDb()
        var familyMemberList:[Contact] = try db.getObjects(on: Contact.CodingKeys.all, fromTable: Contact.table,where: Contact.CodingKeys.deleted == false && Contact.CodingKeys.family == true)
        return familyMemberList
    }
}
